package dto

import (
	"danmu/common"
	"danmu/model"
	"time"
)

type CommentDto struct {
	Cid uint `json:"cid"`
	Uid uint `json:"uid"`
	Text string `json:"text"`
	Name string `json:"name"`
	Face string `json:"face"`
	Reply []Reply `json:"reply"`
	Date time.Time `json:"date"`
}

type Reply struct {
	Cid uint `json:"cid"`
	Uid uint `json:"uid"`
	LastId uint `json:"last_id"`
	Text string `json:"text"`
	Name string `json:"name"`
	Face string `json:"face"`
	ReplyUid uint `json:"reply_uid"`
	ReplyName string `json:"reply_name"`
	Date time.Time `json:"date"`
}

func ToCommentDto(comment []model.Comment,count int) []CommentDto {
	DB :=common.GetDB()
	//储存回复和回复数量
	var reply []model.Comment
	var replyCount int
	newComment := make([]CommentDto,count)
	for i:= 0;i < count;i++{
		newComment[i].Cid = comment[i].ID
		newComment[i].Uid = comment[i].Uid
		newComment[i].Text = comment[i].Text
		newComment[i].Date = comment[i].CreatedAt
		newComment[i].Name = comment[i].Author.Name
		newComment[i].Face = comment[i].Author.Face

		//处理回复
		DB.Where("vid = ? AND last_id = ? ", comment[i].Vid, comment[i].ID).Preload("Author").Find(&reply).Count(&replyCount)
		newReply := make([]Reply,replyCount)
		for j:= 0;j < replyCount;j++{
			newReply[j].Cid = reply[j].ID
			newReply[j].LastId = reply[j].LastId
			newReply[j].Text =reply[j].Text
			newReply[j].Uid = reply[j].Uid
			newReply[j].Date = reply[j].CreatedAt
			newReply[j].Name = reply[j].Author.Name
			newReply[j].Face = reply[j].Author.Face
			newReply[j].ReplyUid = reply[j].ReplyUid
			newReply[j].ReplyName = reply[j].ReplyName
		}
		newComment[i].Reply = newReply
	}
	return newComment
}


